package com.moody.service.impl;

import java.util.List;

import javax.servlet.ServletContext;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;
import org.springframework.web.context.ContextLoader;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.moody.hibernate.dao.CodeLibDAO;
import com.moody.hibernate.dao.CustAddressDAO;
import com.moody.hibernate.dao.MainscaleHisDAO;
import com.moody.hibernate.dao.MainscaleInfoDAO;
import com.moody.hibernate.dao.ModelHisDAO;
import com.moody.hibernate.dao.ModelInfoDAO;
import com.moody.hibernate.dao.ModelLevelDAO;
import com.moody.hibernate.domain.CodeLib;
import com.moody.hibernate.domain.MainscaleHis;
import com.moody.hibernate.domain.MainscaleInfo;
import com.moody.hibernate.domain.ModelHis;
import com.moody.hibernate.domain.ModelInfo;
import com.moody.hibernate.domain.ModelLevel;
import com.moody.service.ModelService;
/*
 * Copyright © Moody analytics
 *
 * @Author: Wang Fuqiang
 * @Time: 2011-11-28 下午02:01:04
 * @Modified by: 
 * @Description: 
 */
@Service
public class ModelServiceImpl implements ModelService {
	@SuppressWarnings("unused")
	private static final Logger log = LoggerFactory.getLogger(ModelServiceImpl.class);
	WebApplicationContext web = ContextLoader.getCurrentWebApplicationContext();
	ServletContext context = web.getServletContext();  
	WebApplicationContext ctx  = WebApplicationContextUtils.getWebApplicationContext(context);
	
	private ModelHisDAO modelHisDAO = ModelHisDAO.getFromApplicationContext(ctx);
	private ModelInfoDAO modelInfoDAO = ModelInfoDAO.getFromApplicationContext(ctx);
	private ModelLevelDAO modelLevelDAO = ModelLevelDAO.getFromApplicationContext(ctx);
	private MainscaleHisDAO mainScaleHisDAO =  MainscaleHisDAO.getFromApplicationContext(ctx);
	private MainscaleInfoDAO mainScaleInfoDAO =  MainscaleInfoDAO.getFromApplicationContext(ctx);
	private CodeLibDAO codeLibDAO =  CodeLibDAO.getFromApplicationContext(ctx);
	
	@SuppressWarnings("unchecked")
	public List<ModelHis> findModelHis() {
		return modelHisDAO.findAll();
	}

	public void deleteModel(String modelId) {
		ModelHis model = modelHisDAO.findById(modelId);
		if(model==null){
			log.info("modelId: "+modelId+" 的版本不存在");
		}else{
			modelHisDAO.delete(model);
		}
	}

	public List<ModelHis> findNotReleasedModelHis() {
		return modelHisDAO.findNotReleasedModel();
	}

	public List<ModelHis> findNonNotReleasedModelHis() {
		return modelHisDAO.findNonNotReleasedModel();
	}


	public ModelHis findModelHisById(String modelId) {
		
		return modelHisDAO.findById(modelId);
	}

	public void updateModelHis(ModelHis model) {
		modelHisDAO.update(model);
		
	}

	public ModelHis findModelByKind1AndKind2AndState(String kind1, String kind2, String state) {
		return modelHisDAO.findByKind1AndKind2AndState(kind1, kind2, state);
	}

	public boolean findModelByKind1AndKind2AndVersion(String kind1,
			String kind2, String version) {
		return modelHisDAO.findModelByKind1AndKind2AndVersion(kind1,kind2,version);
	}

	public void insertModel(ModelHis model) {
		modelHisDAO.save(model);
	}

	public List<ModelInfo> findModelInfoByModelIdAndDataFlag(String modelId,String dataFlag) {
		return modelInfoDAO.findModelInfoByModelIdAndDataFlag(modelId, dataFlag);
	}

	public void insertModelInfo(List<ModelInfo> list) {
		modelInfoDAO.insertModelInfo(list);
		
	}

	public List<ModelInfo> findModelInfoByModelIdAndDescribe2AndDataFlagAndModifiedField(
			String modelId, String describe2, String dataFlag, String field, String value) {
		return modelInfoDAO.findByModelIdAndDescribe2AndDataFlagAndModifiedField(modelId, describe2, dataFlag, field, value);
	}

	public void updateModelInfo(ModelInfo mi) {
		modelInfoDAO.update(mi);
		
	}


	public List<ModelLevel> findModelLevelByModelId(String modelId) {
		
		return modelLevelDAO.findByModelId(modelId);
	}


	public void insertModelInfo(ModelInfo modelInfo) {
		modelInfoDAO.save(modelInfo);
	}

	public List<ModelInfo> findModelInfoByModelIdAndDescribe2AndDataFlag(
			String modelId, String describe2, String dataFlag, String field) {
		return modelInfoDAO.findByModelIdAndDescribe2AndDataFlagAndModifiedField(modelId, describe2, dataFlag, field);
	}

	public void insertModelLevel(List<ModelLevel> list) {
		modelLevelDAO.insert(list);
		
	}


	public List<MainscaleHis> findNotReleasedMainScale() {
		// TODO Auto-generated method stub
		return mainScaleHisDAO.findNotReleasedMainscale();
	}

	public List<ModelInfo> findModelInfoByModelId(String modelId) {
		return modelInfoDAO.findModelInfoByModelId(modelId);
	}


	public List<MainscaleHis> findNonNotReleasedMainScale() {
		// TODO Auto-generated method stub
		return mainScaleHisDAO.findNonNotReleasedModel();
	}

	public MainscaleHis findMainscaleHisById(String mainScaleId) {
		// TODO Auto-generated method stub
		return mainScaleHisDAO.findById(mainScaleId);
	}

	public MainscaleHis findMainscaleByState(String mainScaleState) {
		// TODO Auto-generated method stub
		return mainScaleHisDAO.findByState(mainScaleState);
	}

	public void updateMainscaleHis(MainscaleHis mainScaleHis) {
		
		mainScaleHisDAO.update(mainScaleHis);
	}

	public List<MainscaleInfo> findMainscaleInfoByMainscaleId(String mainScaleId) {
		// TODO Auto-generated method stub
		return mainScaleInfoDAO.findByMainScaleId(mainScaleId);
	}

	public void insertMainScaleInfo(List<MainscaleInfo> list) {
		mainScaleInfoDAO.insert(list);
		
	}


	public void deleteModelInfo(List<ModelInfo> nowList) {
		modelInfoDAO.deleteModelInfo(nowList);
	}

	public List<MainscaleHis> findMainScaleByVersionAndName(String version,
			String name) {
		
		return mainScaleHisDAO.findByVersionAndName(version, name);
	}

	public void insertMainscale(MainscaleHis mainscale) {
		mainScaleHisDAO.save(mainscale);
	}

	public MainscaleInfo findMainscaleInfoById(String serialNo) {
		return mainScaleInfoDAO.findById(serialNo);
	}

	public void updateMainscaleInfo(MainscaleInfo mainscaleInfo) {
		mainScaleInfoDAO.update(mainscaleInfo);
		
	}

	public List<ModelHis> findEffectedModelHis() {
		return modelHisDAO.findEffectedModelHis();
	}

	public ModelInfo findModelInfoById(String serialNo) {
		return modelInfoDAO.findById(serialNo);
	}

	public void updateCodeLib(List<CodeLib> list) {
		codeLibDAO.updateCodeLib(list);
	}


}
